{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Introduction to Recursion\n",
    "\n",
    "In this lecture we will go over the basics of Recursion.\n",
    "\n",
    "## What is Recursion?\n",
    "\n",
    "There are two main instances of recursion. The first is when recursion is used as a technique in which a function makes one or more calls to itself. The second is when a data structure uses smaller instances of the exact same type of data structure when it represents itself. Both of these instances are use cases of recursion.\n",
    "\n",
    "Recursion actually occurs in the real world, such as fractal patterns seen in plants!\n",
    "\n",
    "## Why use Recursion?\n",
    "\n",
    "Recursion provides a powerful alternative for performing repetitions of tasks in which a loop is not ideal. Most modern programming languages support recursion and recursion serves as a great tool for building out particular data structures.\n",
    "\n",
    "We'll start this introduction with an example of recursion- a factorial function.\n",
    "\n",
    "_______\n",
    "# Factorial Example\n",
    "\n",
    "In this part of the lecture we will explain recursion through an example excercise of creating the factorial function.\n",
    "The factorial function is denoted with an exclamation point and is defined as the product of the integers from 1 to *n*. Formally, we can state this as:\n",
    "\n",
    "$$ n! = n·(n-1)·(n-2)... 3·2·1 $$\n",
    "\n",
    "Note, **if n = 0, then n! = 1**. This is important to take into account, because it will serve as our *base case*. "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Take this example:\n",
    "$$4! = 4 · 3 · 2 · 1 = 24. $$\n",
    "So how can we state this in a recursive manner? This is where the concept of **base case** comes in.\n",
    "\n",
    "**Base case** is a key part of understanding recursion, especially when it comes to having to solve interview problems dealing with recursion. Let's rewrite the above equation of 4! so it looks like this:\n",
    "\n",
    "$$ 4! = 4 · (3 · 2 · 1) = 24 $$\n",
    "\n",
    "Notice that this is the same as:\n",
    "\n",
    "$$ 4! = 4 · 3! = 24 $$\n",
    "\n",
    "Meaning we can rewrite the formal recursion definition in terms of recursion like so:\n",
    "\n",
    "$$ n! = n·(n−1)!$$\n",
    "\n",
    "Note, **if n = 0, then n! = 1**. This means the **base case** occurs once n=0, the *recursive cases* are defined in the equation above. Whenever you are trying to develop a recursive solution it is very important to think about the base case, as your solution will need to return the base case once all the recursive cases have been worked through. Let's look at how we can create the factorial function in Python:\n",
    "___"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "def fact(n):\n",
    "    '''\n",
    "    Returns factorial of n (n!).\n",
    "    Note use of recursion\n",
    "    '''\n",
    "    # BASE CASE!\n",
    "    if n == 0:\n",
    "        return 1\n",
    "    \n",
    "    # Recursion!\n",
    "    else:\n",
    "        return n * fact(n-1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Let's see it in action!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "120"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fact(5)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "Note how we had an if statement to check if a base case occured. Without it this function would not have successfully completed running. We can visualize the recursion with the following figure:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<img src=\"http://faculty.cs.niu.edu/~freedman/241/241notes/recur.gif\"/>"
      ],
      "text/plain": [
       "<IPython.core.display.Image object>"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from IPython.display import Image\n",
    "from IPython.core.display import HTML \n",
    "Image(url= 'http://faculty.cs.niu.edu/~freedman/241/241notes/recur.gif')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "We can follow this flow chart from the top, reaching the base case, and then working our way back up.\n",
    "\n",
    "# Conclusion\n",
    "\n",
    "Recursion is a powerful tool, but it can be a tricky concept to implement. In the next lectures we will go over a few more example problems for recursion. Then afterwards you'll be faced with some real interview questions involving recursion!"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
